
package com.ruoyi.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.pay.consts.ApiCodeEnum;
import com.ruoyi.pay.consts.CS;
import com.ruoyi.pay.domain.MchApp;
import com.ruoyi.pay.domain.MchInfo;
import com.ruoyi.pay.domain.PayInterfaceConfig;
import com.ruoyi.pay.domain.PayInterfaceDefine;
import com.ruoyi.pay.mapper.MchInfoMapper;
import com.ruoyi.pay.mapper.PayInterfaceConfigMapper;
import com.ruoyi.pay.mapper.PayInterfaceDefineMapper;
import com.ruoyi.pay.service.MchAppService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 支付接口配置参数表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-04-27
 */
@Service
public class PayInterfaceConfigService {


    @Autowired
    private MchAppService mchAppService;

    @Resource
    private MchInfoMapper machInfoMapper;


    @Resource
    private PayInterfaceConfigMapper payInterfaceConfigMapper;

    @Resource
    private PayInterfaceDefineMapper payInterfaceDefineMapper;


    @Resource
    private PayInterfaceDefineMapper payInterfaceDefineService;


    /**
     * @Author: ZhuXiao
     * @Description: 根据 账户类型、账户号、接口类型 获取支付参数配置
     * @Date: 17:20 2021/4/27
     */
    public PayInterfaceConfig getByInfoIdAndIfCode(Byte infoType, String infoId, String ifCode) {
         Wrapper<PayInterfaceConfig> eq = PayInterfaceConfig.gw()
            .eq(PayInterfaceConfig::getInfoType, infoType)
            .eq(PayInterfaceConfig::getInfoId, infoId)
            .eq(PayInterfaceConfig::getIfCode, ifCode);
         PayInterfaceConfig payInterfaceConfig = payInterfaceConfigMapper.selectOne(eq);
        return payInterfaceConfig;
    }

    /**
     * @Author: ZhuXiao
     * @Description: 根据 账户类型、账户号 获取支付参数配置列表
     * @Date: 14:19 2021/5/7
     */
    public List<PayInterfaceDefine> selectAllPayIfConfigListByIsvNo(Byte infoType, String infoId) {

        // 支付定义列表
        LambdaQueryWrapper<PayInterfaceDefine> queryWrapper = PayInterfaceDefine.gw();
        queryWrapper.eq(PayInterfaceDefine::getState, CS.YES);
        //queryWrapper.eq(PayInterfaceDefine::getIsIsvMode, CS.YES); // 支持服务商模式

        List<PayInterfaceDefine> defineList = payInterfaceDefineService.selectList(queryWrapper);

        // 支付参数列表
        LambdaQueryWrapper<PayInterfaceConfig> wrapper = PayInterfaceConfig.gw();
        wrapper.eq(PayInterfaceConfig::getInfoId, infoId);
        wrapper.eq(PayInterfaceConfig::getInfoType, infoType);
        List<PayInterfaceConfig> configList = payInterfaceConfigMapper.selectList(wrapper);

        for (PayInterfaceDefine define : defineList) {
            JSONObject ext = new JSONObject();
            // 默认状态
            ext.put("ifConfigState", 0);
            define.setExt(ext);
            for (PayInterfaceConfig config : configList) {
                if (define.getIfCode().equals(config.getIfCode())) {
                    ext.put("ifConfigState", config.getState()); // 配置状态
                    define.setExt(ext);
                }
            }
        }
        return defineList;
    }


    public List<PayInterfaceDefine> selectAllPayIfConfigListByAppId(String appId) {

        MchApp mchApp = mchAppService.getById(appId);
        if (mchApp == null|| mchApp.getState() != CS.YES) {
            throw new ServiceException(ApiCodeEnum.SYS_OPERATION_FAIL_SELETE.getMsg());
        }
        MchInfo mchInfo = machInfoMapper.selectById(mchApp.getMchNo());
        if (mchInfo == null || mchInfo.getState() != CS.YES) {
            throw new ServiceException(ApiCodeEnum.SYS_OPERATION_FAIL_SELETE.getMsg());
        }
        // 支付定义列表
        LambdaQueryWrapper<PayInterfaceDefine> queryWrapper = PayInterfaceDefine.gw();
        queryWrapper.eq(PayInterfaceDefine::getState, CS.YES);

        Map<String, PayInterfaceConfig> isvPayConfigMap = new HashMap<>(); // 服务商支付参数配置集合

        // 根据商户类型，添加接口是否支持该商户类型条件
       /* if (mchInfo.getType() == CS.MCH_TYPE_NORMAL) {
            queryWrapper.eq(PayInterfaceDefine::getIsMchMode, CS.YES); // 支持普通商户模式
        }
        if (mchInfo.getType() == CS.MCH_TYPE_ISVSUB) {
            queryWrapper.eq(PayInterfaceDefine::getIsIsvMode, CS.YES); // 支持服务商模式
            // 商户类型为特约商户，服务商应已经配置支付参数
            List<PayInterfaceConfig> isvConfigList = payInterfaceConfigMapper.selectList(PayInterfaceConfig.gw()
                .eq(PayInterfaceConfig::getInfoId, mchInfo.getIsvNo())
                .eq(PayInterfaceConfig::getInfoType, CS.INFO_TYPE_ISV)
                .eq(PayInterfaceConfig::getState, CS.YES)
                .ne(PayInterfaceConfig::getIfParams, "")
                .isNotNull(PayInterfaceConfig::getIfParams));

            for (PayInterfaceConfig config : isvConfigList) {
                config.addExt("mchType", mchInfo.getType());
                isvPayConfigMap.put(config.getIfCode(), config);
            }
        }*/

        List<PayInterfaceDefine> defineList = payInterfaceDefineMapper.selectList(queryWrapper);

        // 支付参数列表
        LambdaQueryWrapper<PayInterfaceConfig> wrapper = PayInterfaceConfig.gw();
        wrapper.eq(PayInterfaceConfig::getInfoId, appId);
        wrapper.eq(PayInterfaceConfig::getInfoType, CS.INFO_TYPE_MCH_APP);
        List<PayInterfaceConfig> configList = payInterfaceConfigMapper.selectList(wrapper);

        for (PayInterfaceDefine define : defineList) {
             JSONObject ext = new JSONObject();
             ext.put("mchType", mchInfo.getType()); // 所属商户类型

            for (PayInterfaceConfig config : configList) {
                if (define.getIfCode().equals(config.getIfCode())) {
                    ext.put("ifConfigState", config.getState()); // 配置状态
                }
            }

            if (mchInfo.getType() == CS.MCH_TYPE_ISVSUB && isvPayConfigMap.get(define.getIfCode()) == null) {
                ext.put("subMchIsvConfig", CS.NO); // 特约商户，服务商支付参数的配置状态，0表示未配置
            }
            define.setExt(ext);
        }
        return defineList;
    }



    /** 查询商户app使用已正确配置了通道信息 */
    public boolean mchAppHasAvailableIfCode(String appId, String ifCode){

        return payInterfaceConfigMapper.selectCount(
            PayInterfaceConfig.gw()
                .eq(PayInterfaceConfig::getIfCode, ifCode)
                .eq(PayInterfaceConfig::getState, CS.PUB_USABLE)
                .eq(PayInterfaceConfig::getInfoId, appId)
                .eq(PayInterfaceConfig::getInfoType, CS.INFO_TYPE_MCH_APP)
        ) > 0;

    }



}
